<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="员工姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="queryDept">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Emps" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="90" sortable>
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="90"  sortable>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="90"  sortable>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="180"  sortable>
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"  sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="120" sortable>
        <template scope="scope"><!-- scope作用 就是用来获取当前行的值 -->
          <!-- v-if v-show-->
          <div v-if="scope.row.state == 0"  slot="reference" >
            <el-tag size="medium" type="danger">禁用</el-tag>
          </div>
          <div v-if="scope.row.state == 1" slot="reference" >
            <el-tag size="medium" type="success">启用</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="shop.name" label="店铺名" width="160" sortable>
      </el-table-column>
      <el-table-column prop="department.name" label="上级部门" min-width="160" sortable>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :current-page="page" :page-size="pageSize" :total="total" style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="员工名" prop="username">
          <el-input v-model="editForm.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input-number v-model="editForm.age" auto-complete="off"></el-input-number>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="editForm.state">
            <el-radio class="radio" :label="0">禁用</el-radio>
            <el-radio class="radio" :label="1">启用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上级部门">
          <!--<el-input v-model="editForm.parent_id" :min="0" :max="200"></el-input>-->
          <div class="block">
            <el-cascader
                v-model="editForm.department"
                :options="options"
                :props="{ checkStrictly: true,
                          value:'id',    //选中后返回的值
                          label:'name'   //显示的值
                        }"
                clearable></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
export default {
  data() {
    return {
      filters: {
        keyword: ''
      },
      //装员工的数据
      employees: [],
      //树的部门
      options: [],
      Emps: [],
      total: 0,
      pageSize: 5,
      page: 1,
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      title: "",
      editLoading: false,
      editFormRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'},
          {type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change']}
        ]
      },
      //编辑界面数据
      editForm: {
        id: 0,
        username: '',
        age: '',
        email: '',
        department: [],
        state: 0,
      }
    }

  },
  methods: {
    handleCurrentChange(val) {
      this.page = val;
      this.getEmps();
    },
    queryDept() {
      this.page = 1
      this.getEmps();
    },
    //获取部门列表
    getEmps() {
      let para = {
        urrentPage: this.page,
        keyword: this.filters.keyword,
        pageSize: this.pageSize
      };
      this.listLoading = false;
      this.$http.post("/emp/pagelist", para).then(res => {
        this.total = res.data.total;
        this.Emps = res.data.data;
        this.listLoading = false;
      })
    },
    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;//开启忙等
        this.$http.delete("/emp/" + row.id).then(res => {
          this.listLoading = false;//关闭忙等
          if (res.data.success) {//成功的情况
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
          } else {//失败的情况
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
          this.page = 1 //回到首页
          this.getEmps();//重新加载页面
        })
      }).catch(() => {
      });
    },
    //编辑页面弹窗
    handleEdit: function (index, row) {
      this.title = "编辑"
      if(this.$refs['editForm']){
        this.$refs['editForm'].resetFields();//重置表单验证
      }
      this.editFormVisible = true;//是否显示
      this.editForm = Object.assign({}, row); //给表单赋值
    },
    //显示新增界面
    handleAdd: function () {
      this.title = "新增"
      if(this.$refs['editForm']){
        this.$refs['editForm'].resetFields();//重置表单验证
      }
      this.editForm = {
        sn:"",
        username: '',
        age: '',
        email: '',
        department:"",
        state: 0
      }
      this.editFormVisible = true
    },
    //修改/编辑
    //编辑
    editSubmit: function () {
      //手动触发验证规则
      this.$refs.editForm.validate((valid) => {
        //valid 判断你的验证是否通过  true  false
        if (valid) {
          this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
            this.editLoading = true;
            //封装为json了
            let para = Object.assign({}, this.editForm);//克隆数据
            if(para.department){//有值
              //将数组转为对象
              para.department_id = para.department[para.department.length - 1]
              para.department = null
            }
            this.$http.put("/emp",para).then(res=>{
              this.editLoading = false;
              if(res.data.success){//成功的情况
                this.$message({
                  message: res.data.msg,
                  type: 'success'
                });
              }else{//失败的情况
                this.$message({
                  message: res.data.msg,
                  type: 'error'
                });
              }
              //可以不设置   疑问  如果当前页数据被删除完成了怎么处理
              this.page = 1 //回到首页
              this.getEmps();//重新加载页面
            })
            this.editFormVisible = false;
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {

      var ids = this.sels.map(item => item.id);//返回一个ids数组   tostring 直接把数组变为字符串

      this.$confirm('确认删除选中记录吗？', '温馨提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;//开启忙等

        this.$http.patch("/dept/del",ids).then(res=>{
          this.listLoading = false;
          //判断是否删除成功
          if(res.data.success){
            //弹出提示框
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
            //刷新列表
            this.currentPage = 1
            this.getdepts();//刷新列表
          }else {
            //删除失败 提示信息
            this.$message({
              message: res.data.msg,
              type: 'warning'
            });
          }
        })
      }).catch(() => {

      });
    }
  },
    mounted() {
    this.getEmps();

    }

}
</script>

<style scoped>

</style>